<template>
    <div>
        <el-container class="home-container">
            <el-header>
                <div>
                    <img src="../assets/heima.png">
                    <span>后台管理系统</span>
                </div>
                <el-button type="info" @click="logout">退出</el-button>
            </el-header>
            <el-container>
                <el-aside :width="collapses ? '64px' : '200px'">
                        <el-menu
                                router
                                :collapse-transition="false"
                                :collapse="collapses"
                                unique-opened
                                :default-active="activePath"
                                class="el-menu-vertical-demo"
                                background-color="#545c64"
                                text-color="#fff"
                                active-text-color="#3f9aff">

                            <div class="toggle-button" @click="toggleCollapse" >|||</div>

                            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">

                                <template slot="title">
                                    <i :class="iconsObj[item.id]"></i>
                                    <span>{{item.authName}}</span>
                                </template>
                                <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
                                    <template slot="title">
                                        <i class="el-icon-menu"></i>
                                        <span>{{subitem.authName}}</span>
                                    </template>

                                </el-menu-item>
                            </el-submenu>
                        </el-menu>

                </el-aside>
                <el-main>
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>

    </div>
</template>

<script>
    export default {
        name: "Home",
        data(){
            return{
                menulist:[],
                iconsObj:{
                    125:'iconfont icon-user',
                    103:'iconfont icon-tijikongjian',
                    101:'iconfont icon-shangpin',
                    102:'iconfont icon-danju',
                    145:'iconfont icon-baobiao',
                },
                collapses: false,
                activePath:''
            }
        },
        created() {
            this.getMenuList()
            this.activePath = window.sessionStorage.getItem('activePath')
        },
        components: {
        },
        methods:{
            logout() {
                window.sessionStorage.clear()
                this.$router.push('/login')
            },
            async getMenuList(){
                const {data : res} = await this.$http.get('menus')
                if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.menulist = res.data
                console.log(res)
            },
            toggleCollapse() {
                this.collapses = !this.collapses
            },
            saveNavState(activePath){
                window.sessionStorage.setItem('activePath',activePath)
                this.activePath = activePath
            }
        }
    }
</script>

<style scoped>
    .el-header {
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        color: #ffffff;
        font-size: 20px;
        background-color: #373c41;
    }
    .el-header div{
        display: flex;
        align-items: center;
    }
    .el-aside {
        background-color: #323744;
    }
    .el-main {
        background-color: #eaedf2;

    }
    .el-menu {
        border-right: none;
        user-select:none;
    }
    .home-container {
        height: 100vh;
    }
    .toggle-button {
        font-size: 10px;
        color: #ffffff;
        background-color: #485164;
        text-align: center;
        line-height: 25px;
        letter-spacing: 0.2em;
        cursor: pointer;
        height: 25px;

    }
</style>